/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sistemperpustakaan.controller;

import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import sistemperpustakaan.entity.User;
import sistemperpustakaan.entity.UserType;
import sistemperpustakaan.entity.ViewUser;
import sistemperpustakaan.model.UserModel;
import sistemperpustakaan.tablemodel.UserTableModel;
import sistemperpustakaan.tablemodel.UserTypeTableModel;
import sistemperpustakaan.utility.NumberUtility;
import sistemperpustakaan.utility.UserUtility;
import sistemperpustakaan.view.FrmLogin;
import sistemperpustakaan.view.user.DialogDetailUser;
import sistemperpustakaan.view.user.DialogDetailUserType;
import sistemperpustakaan.view.user.FrmTipeUserView;
import sistemperpustakaan.view.user.FrmUbahPassword;
import sistemperpustakaan.view.user.FrmUserView;

/**
 *
 * @author EKo
 */
public class UserController {

    FrmUserView userView;
    UserModel model;
    FrmTipeUserView typeUserView;
    DialogDetailUserType detailUserType;
    DialogDetailUser detailUser;
    FrmUbahPassword detailUbahPassword;
    ViewUser vUser;
    User user;
    FrmLogin frameLogin;
    UserUtility uu;

    public FrmUbahPassword getDetailUbahPassword() {
        return detailUbahPassword;
    }

    public void setDetailUbahPassword(FrmUbahPassword detailUbahPassword) {
        this.detailUbahPassword = detailUbahPassword;
    }

    public UserController(FrmUbahPassword detailUbahPassword) {
        this.detailUbahPassword = detailUbahPassword;
    }

    public UserController(FrmUserView userView) {
        this.userView = userView;
    }

    public UserController(FrmUserView userView, UserModel model) {
        this.userView = userView;
        this.model = model;
    }

    public UserController(FrmTipeUserView typeUserView, UserModel model) {
        this.typeUserView = typeUserView;
        this.model = model;
    }

    public UserController() {
    }

    public UserModel getModel() {
        return model;
    }

    public void setDetailUser(DialogDetailUser detailUser) {
        this.detailUser = detailUser;
    }

    public FrmTipeUserView getTypeUserView() {
        return typeUserView;
    }

    public void setModel(UserModel model) {
        this.model = model;
    }

    public FrmUserView getUserView() {
        return userView;
    }

    public void fillComboType() {
        Vector vectorType = model.getVectorType();
        userView.getComboType().setModel(new DefaultComboBoxModel(vectorType));
    }

    public void findListUser() {
        userView.setTableModel(new UserTableModel());
        userView.getTableModel().setList(model.getListUser(
                userView.getTxtUserId().getText(),
                userView.getComboType().getSelectedItem().toString(),
                userView.getTxtNama().getText()));
        userView.getTableUser().setModel(userView.getTableModel());
        userView.setTableWidth();
        userView.setIdUser("");

        int jumlah = userView.getTableUser().getRowCount();
        userView.getLblJumlah().setText(Integer.toString(jumlah));
    }

    public void refreshListUser() {
        userView.getTxtUserId().setText("");
        userView.getTxtNama().setText("");
        userView.getComboType().setSelectedIndex(0);
        findListUser();
    }

    public void findListUserType() {
        int idUserType = -1;
        if (!typeUserView.getTxtId().getText().equals("")) {
            try {
                idUserType = Integer.valueOf(typeUserView.getTxtId().getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(typeUserView, "Field ini harus diisi dengan angka");
                typeUserView.getTxtId().setText("");
            }
        }
        typeUserView.setTableModel(new UserTypeTableModel());
        typeUserView.getTableModel().setList(model.getListUserType(idUserType, typeUserView.getTxtNama().getText()));
        typeUserView.getTableType().setModel(typeUserView.getTableModel());
        typeUserView.getTableType().getColumnModel().getColumn(0).setPreferredWidth(80);
        typeUserView.getTableType().getColumnModel().getColumn(1).setPreferredWidth(150);
        typeUserView.getTableType().getColumnModel().getColumn(2).setPreferredWidth(200);

        int jumlah = typeUserView.getTableType().getRowCount();
        typeUserView.getLblJumlah().setText(Integer.toString(jumlah));
    }

    public void refreshListUserType() {
        typeUserView.getTxtId().setText("");
        typeUserView.getTxtNama().setText("");
        findListUserType();
    }

    public void saveDeleteUserType() {
        if (JOptionPane.showConfirmDialog(typeUserView, "Apakah anda yakin akan menghapus data ini?", "Hapus data",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            if (model.deleteUserType(typeUserView.getIdType())) {
                JOptionPane.showMessageDialog(typeUserView, "Hapus data sukses");
                findListUserType();
            } else {
                JOptionPane.showMessageDialog(typeUserView, "Hapus data gagal");
            }
        }
    }

    public void setDetailUserType(DialogDetailUserType detailUserType) {
        this.detailUserType = detailUserType;
    }

    public void showDetailUserType(int idType) {
        UserType userType = model.getUserType(idType);
        detailUserType.getTxtId().setText(userType.getId() + "");
        detailUserType.getTxtNama().setText(userType.getNama());
        String privilege = userType.getPrivilege();
        detailUserType.getTablePrivilege().setValueAt(isTrue(privilege.substring(1, 2)), 1, 1);
        for (int i = 0; i < 13; i++) {
            detailUserType.getTablePrivilege().setValueAt(isTrue(privilege.substring(i, i + 1)), i, 1);
        }
    }

    public boolean isTrue(String p) {
        if (p.equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    public void saveNewUserType() {
        if (detailUserType.getTxtNama().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUserType, "Nama Type User masih kosong");
            detailUserType.getTxtNama().requestFocus();
        } else if (detailUserType.getTxtId().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUserType, "ID Type User masih kosong");
            detailUserType.getTxtId().requestFocus();
        } else {
            String privilege = createPrivilge();
            if (model.insertTypeUser(Integer.valueOf(detailUserType.getTxtId().getText()), detailUserType.getTxtNama().getText(), privilege)) {
                JOptionPane.showMessageDialog(detailUserType, "Simpan type baru sukses");
                findListUserType();
                detailUserType.dispose();
            } else {
                JOptionPane.showMessageDialog(detailUserType, "Simpan type baru gagal");
            }
        }
    }

    public void saveUpdateUserType() {
        if (detailUserType.getTxtNama().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUserType, "Nama Type User masih kosong");
            detailUserType.getTxtNama().requestFocus();
        } else if (detailUserType.getTxtId().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUserType, "ID Type User masih kosong");
            detailUserType.getTxtId().requestFocus();
        } else if (!NumberUtility.isNumberValid(detailUserType.getTxtId().getText())) {
            JOptionPane.showMessageDialog(detailUserType, "ID Type Tidak Valid");
            detailUserType.getTxtId().requestFocus();
        } else {
            String privilege = createPrivilge();
            int id = Integer.valueOf(detailUserType.getTxtId().getText());
            if (model.updateTypeUser(id, detailUserType.getTxtNama().getText(), privilege, typeUserView.getIdType())) {
                JOptionPane.showMessageDialog(detailUserType, "Update type baru sukses");
                findListUserType();
                detailUserType.dispose();
            } else {
                JOptionPane.showMessageDialog(detailUserType, "Update type baru gagal");
            }
        }
    }

    private String createPrivilge() {
        String priv = "";
        for (int i = 0; i < 13; i++) {
            if (Boolean.valueOf(detailUserType.getTablePrivilege().getValueAt(i, 1).toString())) {
                priv += "1";
            } else {
                priv += "0";
            }
        }
        return priv;
    }

    public void showGenerateIdUserType() {
        UserType usertype = model.generateIdUserType();
        detailUserType.getTxtId().setText(usertype.getId() + "");
    }

    public void showDetailUser(String userId) {
        user = model.getUser(userId);
        detailUser.getTxtUserId().setText(user.getUserId());
        detailUser.getTxtNamaUser().setText(user.getNama());
        detailUser.getTxtPassword().setText(user.getPassword());
        setSelectedType(user.getType());
    }

    public void saveNewUser() {
        if (detailUser.getTxtUserId().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "User Id Masih kosong");
        } else if (detailUser.getTxtNamaUser().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "Nama user Masih kosong");
        } else if (detailUser.getTxtIdType().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "Type User Masih kosong");
        } else if (detailUser.getTxtPassword().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "Password Masih kosong");
        } else {
            if (model.insertUser(
                    detailUser.getTxtUserId().getText(),
                    Integer.valueOf(detailUser.getTxtIdType().getText()),
                    detailUser.getTxtPassword().getText(),
                    detailUser.getTxtNamaUser().getText())) {
                JOptionPane.showMessageDialog(detailUser, "Simpan user baru sukses");
                findListUser();
                userView.setTableWidth();
                detailUser.dispose();
            } else {
                JOptionPane.showMessageDialog(detailUser, "Simpan user baru gagal");
            }
        }
    }

    public void setSelectedType(int id) {
        UserType ut = model.getUserType(id);
        detailUser.getTxtIdType().setText(ut.getId() + "");
        detailUser.getTxtNamaType().setText(ut.getNama());
    }

    public void saveUpdateUser() {


        if (detailUser.getTxtUserId().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "User Id Masih kosong");
        } else if (detailUser.getTxtNamaUser().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "Nama user Masih kosong");
        } else if (detailUser.getTxtIdType().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "Type User Masih kosong");
        } else if (detailUser.getTxtPassword().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "Password Masih kosong");
        } else {
            if (model.updateUser(
                    detailUser.getTxtUserId().getText(),
                    Integer.valueOf(detailUser.getTxtIdType().getText()),
                    detailUser.getTxtPassword().getText(),
                    detailUser.getTxtNamaUser().getText(),
                    user.getId())) {
                JOptionPane.showMessageDialog(detailUser, "Update data user  sukses");
                findListUser();
                userView.setTableWidth();
                detailUser.dispose();
            } else {
                JOptionPane.showMessageDialog(detailUser, "Update data user  gagal");
            }
        }
    }

    public void saveDeleteUser() {
        if (JOptionPane.showConfirmDialog(userView, "Apakah anda yakin akan menghapus data ini?", "Hapus user",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            if (model.deleteUser(userView.getIdUser())) {
                JOptionPane.showMessageDialog(detailUser, "Delete user user  sukses");
                findListUser();
            } else {
                JOptionPane.showMessageDialog(detailUser, "Delete user user  gagal");
            }
        }
    }

    public boolean login() {
        if (frameLogin.getTxtUsername().getText().equals("")) {
            JOptionPane.showMessageDialog(frameLogin, "Masukan Username Anda");
            frameLogin.getTxtUsername().requestFocus();
        } else if (frameLogin.getTxtPassword().getText().equals("")) {
            JOptionPane.showMessageDialog(frameLogin, "Masukan Password Anda");
            frameLogin.getTxtPassword().requestFocus();
        } else {
            String userName = frameLogin.getTxtUsername().getText();
            String password = frameLogin.getTxtPassword().getText();
            if (model.isRegistered(userName, password)) {
                return true;
            } else {
                JOptionPane.showMessageDialog(frameLogin, "Username atau Password anda salah, \n "
                        + "pastikan anda ketik username dan password dengan benar ");
            }
        }
        return false;
    }

    public void selectUser(String userId, String password) {
        user = model.getUser(userId, password);
        int type = user.getType();
        UserType userType = model.getUserType(type);
        String privilege = userType.getPrivilege();
        uu = new UserUtility(privilege);
    }

    public void setFrameLogin(FrmLogin frameLogin) {
        this.frameLogin = frameLogin;
    }

    public void ubahPassword() {

        String a = detailUbahPassword.getTxtPasswordBaru().getText();
        String b = detailUbahPassword.getTxtKonfirmPassword().getText();
        boolean cek = a.matches(b);

        String idUser = UserUtility.idUser;
        String passwordDb = model.getPassword(idUser);
        String passwordLama = detailUbahPassword.getTxtPasswordLama().getText();
        boolean cekPasswdLama = passwordDb.matches(passwordLama);

        if (detailUbahPassword.getTxtPasswordLama().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "Password Lama Masih kosong");
        } else if (detailUbahPassword.getTxtPasswordBaru().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "Password Baru Masih kosong");
        } else if (detailUbahPassword.getTxtKonfirmPassword().getText().equals("")) {
            JOptionPane.showMessageDialog(detailUser, "Password Baru Masih kosong");
        } else if (!cekPasswdLama) {
            JOptionPane.showMessageDialog(detailUser, "Password Lama Tidak Sesuai");
        } else if (!cek) {
            JOptionPane.showMessageDialog(detailUser, "Password Baru tidak Sesuai");
        } else if (model.ubahPassword(idUser, a)) {
            JOptionPane.showMessageDialog(detailUser, "Password Sukses Diganti");
            detailUbahPassword.getTxtPasswordLama().setText("");
            detailUbahPassword.getTxtPasswordBaru().setText("");
            detailUbahPassword.getTxtKonfirmPassword().setText("");
            detailUbahPassword.dispose();
        } else {

            JOptionPane.showMessageDialog(detailUser, "Simpan user baru gagal");

        }
    }
}
